package com.cloud.cloudvideo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.cloudvideo.entity.ResultModel;
import com.cloud.cloudvideo.entity.TreePojo;
import com.cloud.cloudvideo.entity.SysRole;
import com.cloud.cloudvideo.entity.SysRolePermission;
import com.cloud.cloudvideo.service.RoleService;


/**
 * 角色 管理
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/tolist")
	public String rolelist() {
		return "manager/role/list";
	}
	
	
	@RequestMapping("/list")
	@ResponseBody
	public ResultModel<List<SysRole>> list(){
		List<SysRole> list = roleService.list();
		return ResultModel.successWithData(list, list.size());
	}
	
	@RequestMapping("/toAdd")
	public String toAdd() {
		
		return "manager/role/add";
	}
	
	@RequestMapping("/toModify")
	public String toModify(Integer id,Model model) {
		SysRole role = roleService.roleInfo(id);
		model.addAttribute("role", role);
		return "manager/role/add";
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	public ResultModel add(SysRole role) {
		roleService.modifyRole(role);
		return ResultModel.successWithNoData();
	}
	
	
	@RequestMapping("/del")
	@ResponseBody
	public ResultModel del(Integer id) {
		roleService.del(id);
		return ResultModel.successWithNoData();
	}
	
	
	@RequestMapping("/totreelistselect")
	public String totreelist(Integer roleid,Model model) {
		Integer[] ids  = roleService.allPermissionId(roleid);
		model.addAttribute("roleId", roleid);
		String json = JSONArray.toJSONString(ids);
		model.addAttribute("list",json);
		return "manager/role/treelist-select";
	}
	
	
	/**
	 * 修改角色权限
	 * @return
	 */
	@RequestMapping(value="/modifyRolePremission",consumes=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResultModel modifyRolePermission(@RequestBody List<TreePojo> pojo,@RequestParam("roleId")Integer roleId) {
		roleService.modifyRolePermission(pojo,roleId);
		return ResultModel.successWithNoData();
	}
	
	
	
}
